<?php
/* ******************************
 *	Category aJax actions
 * ******************************
*/

include('../../includes/classes/core.php');
include('../../includes/classes/category.tree.class.php');
include('../../includes/classes/log.class.php');


$log 		= new log();

$action	= $_REQUEST['act'];
$error	= '';
$data	= '';

switch ($action) {
	case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
		
        break;
    case 'get_edit_page':
	    $cat_id		= $_REQUEST['id'];
		$page		= GetPage(GetCategory($cat_id));
        
        $data		= array('page'	=> $page);
        
        break;
 	case 'get_list' :
		$count	= $_REQUEST['count'];
	    $hidden	= $_REQUEST['hidden'];
	    
	    $rResult = mysql_query("SELECT	`prod`.`id`,
                                		`prod`.`name`
							    FROM	`production_category` AS `prod`
	    						WHERE	`actived` = 1");
	    
		$data = array(
			"aaData"	=> array()
		);
		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}
		
        break;
    case 'save_category':
		$cat_id 		= $_REQUEST['id'];
		$image 			= $_REQUEST['img'];
		$checked 		= $_REQUEST['ch'];
		
    	$cat_name		= htmlspecialchars($_REQUEST['cat'], ENT_QUOTES);
		
		if($cat_name != '' && $cat_id == ''){
			if(!CheckCategoryExist($cat_name, $cat_id)){
				AddCategory($cat_name, $image, $checked);
				
				GLOBAL $log;
				$log->setInsertLog('production_category');
				
			} else {
				$error = '"' . $cat_name . '" უკვე არის სიაში!';
			}
		}else{
			SaveCategory($cat_id, $cat_name, $image, $checked);
		}
		
        break;
    case 'disable':
		$cat_id	= $_REQUEST['id'];
		DisableCategory($cat_id);
		
        break;
	
	case 'delete_image':
		$cat_id 		= $_REQUEST['id'];
		DeleteImage($cat_id);
		
		break;
		
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Category Functions
 * ******************************
 */


function DeleteImage($cat_id)
{
	GLOBAL $log;
	$log->setUpdateLogBefore('production_category', $cat_id);
	
	mysql_query("UPDATE
	`production_category`
	SET
	`image`			= NULL
	WHERE
	`id`			= $cat_id");
	
	$log->setUpdateLogAfter('production_category', $cat_id);
}

function AddCategory($cat_name, $image, $checked)
{
	mysql_query("INSERT INTO `production_category`
					(`name`, `image`, `pos_menu_visible`) 
				 VALUES
					('$cat_name', '$image', $checked)");
}

function SaveCategory($cat_id, $cat_name, $image, $checked)
{
	GLOBAL $log;
	$log->setUpdateLogBefore('production_category', $cat_id);
	
	mysql_query("UPDATE
	    			`production_category`
				 SET
				    `name` 				= '$cat_name',
				    `image`     		= '$image',
				    `pos_menu_visible` 	= $checked
				 WHERE
					`id` = $cat_id");
	
	$log->setUpdateLogAfter('production_category', $cat_id);
}

function DisableCategory($cat_id)
{
	GLOBAL $log;
	$log->setUpdateLogBefore('production_category', $cat_id);
	
    mysql_query("UPDATE `production_category`
				 SET    `actived` = 0
				 WHERE	`id` = $cat_id");
    
    $log->setUpdateLogAfter('production_category', $cat_id);
}

function CheckCategoryExist($cat_name, $cat_id) 
{
    $res = mysql_fetch_assoc(mysql_query("SELECT `id`
										  FROM   `production_category`
										  WHERE  `name` = '$cat_name' && `id` = $cat_id && `actived` = 1"));
	if($res['id'] != ''){
		return true;
	}
	return false;
}




function GetCategory($cat_id) 
{
    $res = mysql_fetch_assoc(mysql_query("SELECT `id`,
    											 `name`,
    											 `image`,
    											IF(production_category.pos_menu_visible = 1, 'checked', NULL) AS `val`,
    											`pos_menu_visible`
									      FROM   `production_category`
									      WHERE  `id` = $cat_id" ));
    
	return $res;
}

function GetPage($res = '')
{
	$image = $res[image];
	if(empty($image)){
		$image = '0.jpg';
	}
	
	$data = '
	<div id="dialog-form">
	    <fieldset style="width: 400px; float: left;">
	    	<legend>ძირითადი ინფორმაცია</legend>
	    	
	    	<table class="dialog-form-table">
				<tr>
					<td style="width: 100px;"><label for="category">კატეგორია</label></td>
					<td>
						<input type="text" class="idle seoy-address" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" id="category" value="' . $res['name'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 100px;"><label for="category">სალაროს მენიუში</label></td>
					<td>
						<input type="checkbox" name="visible" class="check" id="checkbox" '. $res['val'] .'/>
					</td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="cat_id" value="' . $res['id'] . '" />
        </fieldset>
		
					
 	    <fieldset style="width: 400px; float: left;">
	    	<legend>პროდუქტის სურათი</legend>
			
	    	<table class="dialog-form-table" width="100%">
	    		<tr>
					<td id="img_colum" colspan="2">
						<img id="upload_img" src="media/uploads/images/category/' . $image . '">
					</td>
				</tr>
				<tr><!-- Upload Image -->
					<td id="act">
						<span>
							<a href="#" id="view_image" class="complate" style="float:left; margin-left:14px;">View</a> <p style="float:left; margin-left:5px;">|</p> <a href="#" id="delete_image" class="delete" style="float:left; margin-left:5px;">Delete</a>
						</span>
					</td>
					<td>
						<div class="file-uploader">
							<input id="choose_file" type="file" name="choose_file" class="input" style="display: none;">
							<button id="choose_button" class="center">აირჩიეთ ფაილი</button>
						</div>
					</td>
				</tr>
			</table>
        </fieldset>					
    </div>
    ';
	return $data;
}

?>